﻿using BuildStatusIndicator.Controller.EventBroker;
using BuildStatusIndicator.Controller.Events;
using Microsoft.Practices.Unity;

namespace BuildStatusIndicator.X10
{
    public class X10BuildIndicator : IListener<BuildSucceededEvent>, IListener<BuildFailedEvent>,
                                     IListener<BuildPartiallySucceededEvent>, IListener<BuildStoppedEvent>,
                                     IListener<BuildInProgressEvent>,
                                     IListener<BuildNotificationEndingEvent>
    {
        private readonly IUnityContainer _container;

        public X10BuildIndicator(IUnityContainer containter, IBuildStatusEventPublisher eventPublisher)
        {
            _container = containter;
            eventPublisher.AddListener(this);
        }

        #region Implementation of IListener<BuildSucceededEvent>

        public void Handle(BuildSucceededEvent subject)
        {
            using (IX10SignalIndicator x10 = GetX10SignalIndicator())
            {
                x10.TurnOffRed();
                x10.TurnOnGreen();
            }
        }

        #endregion

        #region Implementation of IListener<BuildFailedEvent>

        public void Handle(BuildFailedEvent subject)
        {
            using (IX10SignalIndicator x10 = GetX10SignalIndicator())
            {
                x10.TurnOffGreen();
                x10.TurnOnRed();
            }
        }

        #endregion

        #region Implementation of IListener<BuildPartiallySucceededEvent>

        public void Handle(BuildPartiallySucceededEvent subject)
        {
            using (IX10SignalIndicator x10 = GetX10SignalIndicator())
            {
                x10.TurnOffGreen();
                x10.TurnOnRed();
            }
        }

        #endregion

        #region Implementation of IListener<BuildStoppedEvent>

        public void Handle(BuildStoppedEvent subject)
        {
            using (IX10SignalIndicator x10 = GetX10SignalIndicator())
            {
                x10.TurnOffGreen();
                x10.TurnOnRed();
            }
        }

        #endregion

        #region Implementation of IListener<BuildInProgressEvent>

        public void Handle(BuildInProgressEvent subject)
        {
            using (IX10SignalIndicator x10 = GetX10SignalIndicator())
            {
                x10.TurnOnGreen();
                x10.TurnOnRed();
            }
        }

        #endregion

        #region Implementation of IListener<BuildNotificationEndingEvent>

        public void Handle(BuildNotificationEndingEvent subject)
        {
            using (IX10SignalIndicator x10 = GetX10SignalIndicator())
            {
                x10.TurnOffGreen();
                x10.TurnOffRed();
            }
        }

        #endregion

        private IX10SignalIndicator GetX10SignalIndicator()
        {
            return _container.Resolve<IX10SignalIndicator>();
        }
    }
}